import { createRouter, createWebHashHistory } from "vue-router";
import { dataStore } from '@/store/piniastore-data.js';

const routes = [
	{
		path: '/',
		redirect: '/login'
	},
	{
		path: '/',
		component: () => import("@/layout/Layout.vue"),
		children: [
			{
				path: "/speed",
				name: "Speed",
				meta: { title: 'Speed', id: 8 },
				component: () => import("../views/Speed.vue")
			},
			{
				path: "/error",
				name: "Error",
				meta: { title: 'Error', id: 6 },
				component: () => import("../views/Error.vue")
			},
			{
				path: "/resource",
				name: "resource",
				meta: { title: 'resource', id: 6 },
				component: () => import("../views/Resource.vue")
			},
			{
				path: "/useraction",
				name: "UserAction",
				meta: { title: 'UserAction', id: 6 },
				component: () => import("../views/UserAction.vue")
			},
			{
				path: "/overview-drawing",
				name: "overview-drawing",
				meta: { title: '总览图', id: 5 },
				component: () => import("../views/overview-drawing.vue")
			},]
	},

	// 登录页面
	{
		path: "/login",
		name: "Login",
		meta: { title: 'Login' },
		component: () => import("@/views/Login.vue")
	},
	{
		path: "/register",
		name: "Register",
		meta: { title: 'Register' },
		component: () => import("@/views/Register.vue")
	},

	{
		path: '/:pathMatch(.*)',
		name: '404',
		meta: { title: '404界面' },
		component: () => import("../views/404.vue")
	}
]

const router = createRouter({ history: createWebHashHistory(), routes });

router.beforeEach((to, from, next) => {
	/* 每次更改路由前就会修改浏览器上方的网页名字 */
	next();/* 更改后放行路由 */
});
router.beforeEach((to, from, next) => {
	const dStore = dataStore();
	let requestID = dStore.requestID
	document.title = `指针正常|${to.meta.title}`;
	// console.log(!(to.path=="/login"||to.path=="/register"));
	// console.log(!requestID);
	// console.log(!requestID&&!(to.path=="/login"||to.path=="/register"));
	if (!requestID&&!(to.path=="/login"||to.path=="/register")) {
		next("/login");
		alert("请登录")
	} else {
		next();
	}
});
//  else if (to.meta.permission) {
//     // 如果是管理员权限则可进入，这里只是简单的模拟管理员权限而已
//     role === 'admin'
//         ? next()
//         : next('/403');
// }
export default router;